global root "/Volumes/Workgroup/Lab/Lab-heimb/Montenovo/Volunteering Paper/Data"
global merge "$root/05_merged_taxsim_IUPUI_PSID" 
global dictionaries "$root/Dictionaries"

global output_data "$root/06_yearly_analytic_files"

cd "$dictionaries"
capture log close
log using 01_preliminary_regressions_build, replace

*Create after tax rate:
*Create taxprice = 1 if didn't itemize\
*Taxprice = (1 – frate –srate + frate*srate) if itemized
*LHS = 1 if anyone if family volunteers
*RHS = taxprice, income (in $1000s), married dummy, number of children,  education group of head, race and ethnicity of head, state fixed effects, year fixed effects, family fixed effects
*Also run a specification with LHS = total amount of time (drop years that don't have that variable)

// Author: Laura Montenovo
// this do file takes the merged yearly datasets and creates variables that we will use in the regressions (i.e. education for spouse and head, tax price,...)
// it orders the variables all with the same order so that we can append them safely (across the years) for analysis
cd "$merge"


foreach i of numlist 2001 2017 2019 {

cd "$merge"

di `i'

use `i'_IUPUI_taxsim_PSID.dta, clear

drop if year==.

*education variablesr

gen head_married=.
replace head_married=0 if mstat==1
replace head_married=1 if mstat==2

//foreach h of varlist education_wife white_w black_w native_amer_w asian_w other_w hispanic_wife sage  {
    
	//	replace `h'=.a if head_married==0
	
	//}


generate LHS=0 if (education_head!=. & sequence_num==1) | (education_wife!=. & sequence_num==2)
replace LHS=1 if education_head<=11 & sequence_num==1
replace LHS=1 if education_wife<=11 & sequence_num==2

generate HS=0 if (education_head!=. & sequence_num==1) | (education_wife!=. & sequence_num==2)
replace HS=1 if education_head==12 & sequence_num==1
replace HS=1 if education_wife==12 & sequence_num==2

generate some_college=0 if (education_head!=. & sequence_num==1) | (education_wife!=. & sequence_num==2)
replace some_college=1 if (education_head==13 | education_head==14 | education_head==15) & sequence_num==1 & education_head!=.
replace some_college=1 if (education_wife==13 | education_wife==14 | education_wife==15) & sequence_num==2 & education_wife!=.

generate college_grad=0 if (education_head!=. & sequence_num==1) | (education_wife!=. & sequence_num==2)
replace college_grad=1 if education_head==16 & sequence_num==1
replace college_grad=1 if education_wife==16 & sequence_num==2

generate college_plus=0 if (education_head!=. & sequence_num==1) | (education_wife!=. & sequence_num==2)
replace college_plus=1 if education_head==17 & sequence_num==1
replace college_plus=1 if education_wife==17 & sequence_num==2

gen sex_spouse=.
replace sex_spouse=1 if sex_head==2 & head_married==1
replace sex_spouse=2 if sex_head==1 & head_married==1

gen sex=.
replace sex=1 if sex_head==1 & sequence_num==1
replace sex=1 if sex_spouse==1 & sequence_num==2
replace sex=2 if sex_head==2 & sequence_num==1
replace sex=2 if sex_spouse==2 & sequence_num==2

replace sage=. if sage==0
gen age=.
replace age=page if sequence_num==1
replace age=sage if sequence_num==2

//replace frate=frate/100
//replace srate=srate/100 

replace frate=(federalTax_otheritem_plus100-federalTax)/100
replace srate=(stateTax_otheritem_plus100-stateTax)/100

//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
replace frate=0 if frate>0
replace srate=0 if srate>0

sum frate srate

replace frate=abs(frate)
replace srate=abs(srate)

 *generate taxprice 
//gen taxprice=1 if whether_itemized!=1
//replace taxprice=1-frate-srate+(frate*srate) if whether_itemized==1

gen taxprice=1 if deductions_allowed==0
replace taxprice=1-frate-srate+(frate*srate) if deductions_allowed>0

*generate variable for whether anyone in family volunteered
egen whether_volunteer_family=rowmax(Hyear Wyear)

*calculate post-tax income
gen post_tax_income_neg=taxable_inc_hh_gen-federalTax-stateTax
gen zero=0
egen post_tax_income=rowmax(post_tax_income_neg zero) if post_tax_income_neg!=.
replace post_tax_income=post_tax_income/100000

*family income (name of variable for income). put it in tens of thousands
replace taxable_inc_hh_gen=taxable_inc_hh_gen/100000
//format taxable_inc_hh_gen  %9.0g

gen hours_volunteered_family=hoursHLB if head_married==0
replace hours_volunteered_family=hoursHLB+hoursWLB if head_married==1

generate whether_volunteer_person=Hyear if sequence_num==1
replace whether_volunteer_person=Wyear if sequence_num==2

generate hours_volunteer_person=hoursHLB if sequence_num==1
replace hours_volunteer_person=hoursWLB if sequence_num==2

gen religion=religion_hd if sequence_num==1
replace religion=religion_spouse if sequence_num==2
// clean religion 

generate white=0 if (white_h!=. & sequence_num==1) | (white_w!=. & sequence_num==2)
replace white=1 if white_h==1 & sequence_num==1
replace white=1 if white_w==1 & sequence_num==2

generate black=0 if (black_h!=. & sequence_num==1) | (black_w!=. & sequence_num==2)
replace black=1 if black_h==1 & sequence_num==1
replace black=1 if black_w==1 & sequence_num==2

generate native_amer=0 if (native_amer_h!=. & sequence_num==1) | (native_amer_w!=. & sequence_num==2)
replace native_amer=1 if native_amer_h==1 & sequence_num==1
replace native_amer=1 if native_amer_w==1 & sequence_num==2

generate asian=0 if (asian_h!=. & sequence_num==1) | (asian_w!=. & sequence_num==2)
replace asian=1 if asian_h==1 & sequence_num==1
replace asian=1 if asian_w==1 & sequence_num==2

generate other=0 if (other_h!=. & sequence_num==1) | (other_w!=. & sequence_num==2)
replace other=1 if other_h==1 & sequence_num==1
replace other=1 if other_w==1 & sequence_num==2

generate hispanic=0 if (hispanic_head!=. & sequence_num==1) | (hispanic_wife!=. & sequence_num==2)
replace hispanic=1 if hispanic_head==1 & sequence_num==1
replace hispanic=1 if hispanic_wife==1 & sequence_num==2

gen married=0 if  head_married==0
replace married=1 if head_married==1 & sequence_num==1
replace married=1 if sequence_num==2

gen hrs_worked_hd_group=500 if tot_hs_worked_head>0 & tot_hs_worked_head<750
replace hrs_worked_hd_group=1000 if tot_hs_worked_head>=750 & tot_hs_worked_head<1250
replace hrs_worked_hd_group=1500 if tot_hs_worked_head>=1250 & tot_hs_worked_head<1750
replace hrs_worked_hd_group=2000 if tot_hs_worked_head>=1750

gen full_time_hd=.
replace full_time_hd=0 if hrs_worked_hd_group>0 & hrs_worked_hd_group<2000
replace full_time_hd=1 if hrs_worked_hd_group>=2000

gen hrs_worked_spouse_group=500 if tot_hs_worked_spouse>0 & tot_hs_worked_spouse<750
replace hrs_worked_spouse_group=1000 if tot_hs_worked_spouse>=750 & tot_hs_worked_spouse<1250
replace hrs_worked_spouse_group=1500 if tot_hs_worked_spouse>=1250 & tot_hs_worked_spouse<1750
replace hrs_worked_spouse_group=2000 if tot_hs_worked_spouse>=1750

gen full_time_spouse=.
replace full_time_spouse=0 if hrs_worked_spouse_group>0 & hrs_worked_spouse_group<2000
replace full_time_spouse=1 if hrs_worked_spouse_group>=2000

replace hr_wage_hd=. if  hr_wage_hd==998 | hr_wage_hd==999
replace hr_wage_spouse=. if  hr_wage_spouse==998 | hr_wage_spouse==999

replace tot_wage_hd=. if  tot_wage_hd==9999998 | tot_wage_hd==9999999
replace tot_wage_spouse=. if  tot_wage_spouse==9999998 | tot_wage_spouse==9999999

replace hr_wage_hd=tot_wage_hd/hrs_worked_hd_group if (hr_wage_hd==. | hr_wage_hd==0) & tot_wage_hd!=. & tot_wage_hd!=0 & hrs_worked_hd_group!=. & hrs_worked_hd_group!=0
replace hr_wage_spouse=tot_wage_spouse/hrs_worked_spouse_group if (hr_wage_spouse==. | hr_wage_spouse==0) & tot_wage_spouse!=. & tot_wage_spouse!=0 & hrs_worked_spouse_group!=. & hrs_worked_spouse_group!=0

gen hourly_wage_aftertax_hd=.
replace hourly_wage_aftertax_hd=hr_wage_hd if deductions_allowed==0
replace hourly_wage_aftertax_hd=hr_wage_hd * (1-frate-srate+(frate*srate)) if deductions_allowed>0

gen hourly_wage_aftertax_spouse=.
replace hourly_wage_aftertax_spouse=hr_wage_spouse if deductions_allowed==0
replace hourly_wage_aftertax_spouse=hr_wage_spouse * (1-frate-srate+(frate*srate)) if deductions_allowed>0

gen hourly_wage_aftertax=.
replace hourly_wage_aftertax=hourly_wage_aftertax_hd if sequence_num==1
replace hourly_wage_aftertax=hourly_wage_aftertax_spouse if sequence_num==2

replace hourly_wage_aftertax=hourly_wage_aftertax/10

gen full_time=.
replace full_time=full_time_hd if sequence_num==1
replace full_time=full_time_spouse if sequence_num==2

gen unemployed_head=0 if whether_unemployed_head==5 | whether_unemployed_head==8 | whether_unemployed_head==9
replace unemployed_head=1 if whether_unemployed_head==1

tab unemployed_head

gen unemployed_spouse=0 if whether_unemployed_spouse==5 | whether_unemployed_spouse==8 | whether_unemployed_spouse==9
replace unemployed_spouse=1 if whether_unemployed_spouse==1

tab unemployed_spouse

generate unemployed=0 if (unemployed_head==0 & sequence_num==1) | (unemployed_spouse==0 & sequence_num==2)
replace unemployed=1 if unemployed_head==1 & sequence_num==1
replace unemployed=1 if unemployed_spouse==1 & sequence_num==2


keep family_id sequence_num unique_id_crossys married  sex age sex_head sex_spouse sage page Hyear Wyear hoursHLB hoursWLB taxprice whether_volunteer_person hours_volunteer_person whether_volunteer_family hours_volunteered_family taxable_inc_hh_gen post_tax_income education_head education_wife state year  children LHS HS some_college college_grad college_plus white black native_amer asian other hispanic whether_itemized total_giving deductions_allowed federalTax_otheritem_plus100 federalTax frate srate stateTax_otheritem_plus100 stateTax hourly_wage_aftertax full_time religion rural unemployed_head unemployed_spouse unemployed

order family_id sequence_num unique_id_crossys married  sex age sex_head sex_spouse sage page  Hyear Wyear hoursHLB hoursWLB taxprice whether_volunteer_person hours_volunteer_person whether_volunteer_family hours_volunteered_family taxable_inc_hh_gen post_tax_income education_head education_wife state year  children LHS HS some_college college_grad college_plus white black native_amer asian other hispanic whether_itemized total_giving deductions_allowed federalTax_otheritem_plus100 federalTax frate srate stateTax_otheritem_plus100 stateTax hourly_wage_aftertax full_time religion rural unemployed_head unemployed_spouse unemployed

cd "$output_data"

save analytic_file_foryear_`i'.dta, replace
 }
 
/////////////////////////////////////////////////////////////////////////////

foreach i of numlist 2003 2005 {

cd "$merge"

di `i'

use `i'_IUPUI_taxsim_PSID.dta, clear

drop if year==.

*education variablesr
gen head_married=.
replace head_married=0 if mstat==1
replace head_married=1 if mstat==2

	//			foreach h of varlist education_wife white_w black_w native_amer_w asian_w other_w hispanic_wife sage  calcul_yr_hs_reli_wf  calcul_yr_hs_reli_right_wf  your_estim_yr_hs_reli_wf  estim_yr_hs_reli_extra_wf  calcul_yr_hs_youth_wf  calcul_yr_hs_youth_right_wf  your_estim_yr_hs_youth_wf  estim_yr_hs_youth_extra_wf  calcul_yr_hs_senior_wf  calcul_yr_hs_senior_right_wf  your_estim_yr_hs_senior_wf  estim_yr_hs_senior_extra_wf  calcul_yr_hs_health_wf  calcul_yr_hs_health_right_wf  your_estim_yr_hs_health_wf  estim_yr_hs_health_extra_wf  calcul_yr_hs_needy_wf  calcul_yr_hs_needy_right_wf  your_estim_yr_hs_needy_wf  estim_yr_hs_needy_extra_wf  calcul_yr_hs_change_wf  calcul_yr_hs_change_right_wf  your_estim_yr_hs_change_wf  estim_yr_hs_change_extra_wf  calcul_yr_hs_other_wf  calcul_yr_hs_other_right_wf  your_estim_yr_hs_change_wf  estim_yr_hs_other_extra_wf  {
					
			//			replace `h'=.a if head_married==0
					
			//		}


generate LHS=0 if (education_head!=. & sequence_num==1) | (education_wife!=. & sequence_num==2)
replace LHS=1 if education_head<=11 & sequence_num==1
replace LHS=1 if education_wife<=11 & sequence_num==2

generate HS=0 if (education_head!=. & sequence_num==1) | (education_wife!=. & sequence_num==2)
replace HS=1 if education_head==12 & sequence_num==1
replace HS=1 if education_wife==12 & sequence_num==2

generate some_college=0 if (education_head!=. & sequence_num==1) | (education_wife!=. & sequence_num==2)
replace some_college=1 if (education_head==13 | education_head==14 | education_head==15) & sequence_num==1 & education_head!=.
replace some_college=1 if (education_wife==13 | education_wife==14 | education_wife==15) & sequence_num==2 & education_wife!=.

generate college_grad=0 if (education_head!=. & sequence_num==1) | (education_wife!=. & sequence_num==2)
replace college_grad=1 if education_head==16 & sequence_num==1
replace college_grad=1 if education_wife==16 & sequence_num==2

generate college_plus=0 if (education_head!=. & sequence_num==1) | (education_wife!=. & sequence_num==2)
replace college_plus=1 if education_head==17 & sequence_num==1
replace college_plus=1 if education_wife==17 & sequence_num==2

gen religion=religion_hd if sequence_num==1
replace religion=religion_spouse if sequence_num==2
// clean religion 

gen sex_spouse=.
replace sex_spouse=1 if sex_head==2 & head_married==1
replace sex_spouse=2 if sex_head==1 & head_married==1

gen sex=.
replace sex=1 if sex_head==1 & sequence_num==1
replace sex=1 if sex_spouse==1 & sequence_num==2
replace sex=2 if sex_head==2 & sequence_num==1
replace sex=2 if sex_spouse==2 & sequence_num==2


replace sage=. if sage==0
gen age=.
replace age=page if sequence_num==1
replace age=sage if sequence_num==2

//replace frate=frate/100
//replace srate=srate/100


replace frate=(federalTax_otheritem_plus100-federalTax)/100
replace srate=(stateTax_otheritem_plus100-stateTax)/100

replace frate=0 if frate>0
replace srate=0 if srate>0

sum frate srate

replace frate=abs(frate)
replace srate=abs(srate)


 *generate taxprice 
//gen taxprice=1 if whether_itemized!=1
//replace taxprice=1-frate-srate+(frate*srate) if whether_itemized==1

gen taxprice=1 if deductions_allowed==0
replace taxprice=1-frate-srate+(frate*srate) if deductions_allowed>0

*generate variable for whether anyone in family volunteered
egen whether_volunteer_family=rowmax(Hyear Wyear)

*calculate post-tax income
gen post_tax_income_neg=taxable_inc_hh_gen-federalTax-stateTax
gen zero=0
egen post_tax_income=rowmax(post_tax_income_neg zero) if post_tax_income_neg!=.
replace post_tax_income=post_tax_income/100000 

*family income (name of variable for income). put it in thousands
replace taxable_inc_hh_gen=taxable_inc_hh_gen/100000
//format taxable_inc_hh_gen  %9.0g

gen hours_volunteered_family=hoursHLB if head_married==0
replace hours_volunteered_family=hoursHLB+hoursWLB if head_married==1

generate whether_volunteer_person=Hyear if sequence_num==1
replace whether_volunteer_person=Wyear if sequence_num==2

generate hours_volunteer_person=hoursHLB if sequence_num==1
replace hours_volunteer_person=hoursWLB if sequence_num==2

/*
replace whether_donation25_religious=.a if whether_donation25_religious==0 | whether_donation25_religious>6
replace whether_donation25_religious=1 if whether_donation25_religious==1
replace whether_donation25_religious=0 if whether_donation25_religious==5
*/


gen married=0 if  head_married==0
replace married=1 if head_married==1 & sequence_num==1
replace married=1 if sequence_num==2


replace whether_donation_religious=.a if whether_donation_religious==0 | whether_donation_religious>6
replace whether_donation_religious=1 if whether_donation_religious==1
replace whether_donation_religious=0 if whether_donation_religious==5

replace amount_donation_religious=.a if amount_donation_religious==0 | amount_donation_religious>999997

replace whether_donation_combo=.a if whether_donation_combo==0 | whether_donation_combo>6
replace whether_donation_combo=1 if whether_donation_combo==1
replace whether_donation_combo=0 if whether_donation_combo==5

replace amount_donation_combo=.a if amount_donation_combo==0 | amount_donation_combo>999997

replace whether_donation_needy=.a if whether_donation_needy==0 | whether_donation_needy>6
replace whether_donation_needy=1 if whether_donation_needy==1
replace whether_donation_needy=0 if whether_donation_needy==5

replace amount_donation_needy=.a if amount_donation_needy==0 | amount_donation_needy>99997

replace whether_donation_health=.a if whether_donation_health==0 | whether_donation_health>6
replace whether_donation_health=1 if whether_donation_health==1
replace whether_donation_health=0 if whether_donation_health==5

replace amount_donation_health=.a if amount_donation_health==0 | amount_donation_health>99997

replace whether_donation_education=.a if whether_donation_education==0 | whether_donation_education>6
replace whether_donation_education=1 if whether_donation_education==1
replace whether_donation_education=0 if whether_donation_education==5

replace amount_donation_education=.a if amount_donation_education==0 | amount_donation_education>99997

replace whether_donation_youth=.a if whether_donation_youth==0 | whether_donation_youth>6
replace whether_donation_youth=1 if whether_donation_youth==1
replace whether_donation_youth=0 if whether_donation_youth==5

replace amount_donation_youth=.a if amount_donation_youth==0 | amount_donation_youth>99997

replace whether_donation_culture=.a if whether_donation_culture==0 | whether_donation_culture>6
replace whether_donation_culture=1 if whether_donation_culture==1
replace whether_donation_culture=0 if whether_donation_culture==5

replace amount_donation_culture=.a if amount_donation_culture==0 | amount_donation_culture>99997

replace whether_donation_community=.a if whether_donation_community==0 | whether_donation_community>6
replace whether_donation_community=1 if whether_donation_community==1
replace whether_donation_community=0 if whether_donation_community==5

replace amount_donation_community=.a if amount_donation_community==0 | amount_donation_community>99997

replace whether_donation_environment=.a if whether_donation_environment==0 | whether_donation_environment>6
replace whether_donation_environment=1 if whether_donation_environment==1
replace whether_donation_environment=0 if whether_donation_environment==5

replace amount_donation_environment=.a if amount_donation_environment==0 | amount_donation_environment>99997

replace whether_donation_peace=.a if whether_donation_peace==0 | whether_donation_peace>6
replace whether_donation_peace=1 if whether_donation_peace==1
replace whether_donation_peace=0 if whether_donation_peace==5

replace amount_donation_peace=.a if amount_donation_peace==0 | amount_donation_peace>99997

replace whether_donation_other=.a if whether_donation_other==0 | whether_donation_other>6
replace whether_donation_other=1 if whether_donation_other==1
replace whether_donation_other=0 if whether_donation_other==5

replace amount_donation_other=.a if amount_donation_other==0 | amount_donation_other>99997

replace donation_type_other_org=.a if donation_type_other_org==0

/*
1	Religious or spiritual development
2	Youth and family services
3	Education
4	Senior citizens
5	Health care or medical research
6	People in need of food, shelter, or other basic necessities
7	Political/Social change
8	Improving neighborhoods or communities
9	Volunteer Fire/Rescue
10	Arts, culture, and ethnic awareness
11	Preserving the environment
12	International aid or world peace
13	Combination of purposes
14	Fraternal Organization
15	Active Military
16	Veterans
17	Professional Services
18	Other
19	Helping friends/family
0	Inap.: respondent is not head/wife/"wife"; did not make donations in 2004
*/

replace calcul_yr_hs_reli_hd=.a if calcul_yr_hs_reli_hd==0 | calcul_yr_hs_reli_hd>9000
replace calcul_yr_hs_reli_wf=.a if calcul_yr_hs_reli_wf==0 | calcul_yr_hs_reli_wf>9000

replace calcul_yr_hs_reli_right_hd=.a if calcul_yr_hs_reli_right_hd==0 | calcul_yr_hs_reli_right_hd>5
replace calcul_yr_hs_reli_right_wf=.a if calcul_yr_hs_reli_right_wf==0 | calcul_yr_hs_reli_right_wf>5

/*
1	Fewer hours
2	About right
3	More hours
8	DK
9	NA; refused
0	Inap.:
*/

replace your_estim_yr_hs_reli_hd=.a if your_estim_yr_hs_reli_hd==0 | your_estim_yr_hs_reli_hd>9000
replace your_estim_yr_hs_reli_wf=.a if your_estim_yr_hs_reli_wf==0 | your_estim_yr_hs_reli_wf>9000

replace estim_yr_hs_reli_extra_hd=.a if estim_yr_hs_reli_extra_hd==0 | estim_yr_hs_reli_extra_hd>9000
replace estim_yr_hs_reli_extra_wf=.a if estim_yr_hs_reli_extra_wf==0 | estim_yr_hs_reli_extra_wf>9000

/////

replace calcul_yr_hs_reli_hd=.a if calcul_yr_hs_reli_hd==0 
replace calcul_yr_hs_reli_wf=.a if calcul_yr_hs_reli_wf==0 

replace calcul_yr_hs_youth_right_hd=.a if calcul_yr_hs_youth_right_hd==0 | calcul_yr_hs_youth_right_hd>5
replace calcul_yr_hs_youth_right_wf=.a if calcul_yr_hs_youth_right_wf==0 | calcul_yr_hs_youth_right_wf>5

replace your_estim_yr_hs_youth_hd=.a if your_estim_yr_hs_youth_hd==0 | your_estim_yr_hs_youth_hd>9000
replace your_estim_yr_hs_youth_wf=.a if your_estim_yr_hs_youth_wf==0 | your_estim_yr_hs_youth_wf>9000

replace estim_yr_hs_youth_extra_hd=.a if estim_yr_hs_youth_extra_hd==0 | estim_yr_hs_youth_extra_hd>9000
replace estim_yr_hs_youth_extra_wf=.a if estim_yr_hs_youth_extra_wf==0 | estim_yr_hs_youth_extra_wf >9000

/////

replace calcul_yr_hs_senior_hd=.a if calcul_yr_hs_senior_hd==0 
replace calcul_yr_hs_senior_wf=.a if calcul_yr_hs_senior_wf==0 

replace calcul_yr_hs_senior_right_hd=.a if calcul_yr_hs_senior_right_hd==0 | calcul_yr_hs_senior_right_hd>5
replace calcul_yr_hs_senior_right_wf=.a if calcul_yr_hs_senior_right_wf==0 | calcul_yr_hs_senior_right_wf>5

replace your_estim_yr_hs_senior_hd=.a if your_estim_yr_hs_senior_hd==0 | your_estim_yr_hs_senior_hd>9000
replace your_estim_yr_hs_senior_wf=.a if your_estim_yr_hs_senior_wf==0 | your_estim_yr_hs_senior_wf>9000

replace estim_yr_hs_senior_extra_hd=.a if estim_yr_hs_senior_extra_hd==0 | estim_yr_hs_senior_extra_hd>9000
replace estim_yr_hs_senior_extra_wf=.a if estim_yr_hs_senior_extra_wf==0 | estim_yr_hs_senior_extra_wf >9000


/////

replace calcul_yr_hs_health_hd=.a if calcul_yr_hs_health_hd==0 
replace calcul_yr_hs_health_wf=.a if calcul_yr_hs_health_wf==0 

replace calcul_yr_hs_health_right_hd=.a if calcul_yr_hs_health_right_hd==0 | calcul_yr_hs_health_right_hd>5
replace calcul_yr_hs_health_right_wf=.a if calcul_yr_hs_health_right_wf==0 | calcul_yr_hs_health_right_wf>5

replace your_estim_yr_hs_health_hd=.a if your_estim_yr_hs_health_hd==0 | your_estim_yr_hs_health_hd>9000
replace your_estim_yr_hs_health_wf=.a if your_estim_yr_hs_health_wf==0 | your_estim_yr_hs_health_wf>9000

replace estim_yr_hs_health_extra_hd=.a if estim_yr_hs_health_extra_hd==0 | estim_yr_hs_health_extra_hd>9000
replace estim_yr_hs_health_extra_wf=.a if estim_yr_hs_health_extra_wf==0 | estim_yr_hs_health_extra_wf >9000

/////

replace calcul_yr_hs_needy_hd=.a if calcul_yr_hs_needy_hd==0  | calcul_yr_hs_needy_hd>6000
replace calcul_yr_hs_needy_wf=.a if calcul_yr_hs_needy_wf==0  | calcul_yr_hs_needy_wf>6000

replace calcul_yr_hs_needy_right_hd=.a if calcul_yr_hs_needy_right_hd==0 | calcul_yr_hs_needy_right_hd>5
replace calcul_yr_hs_needy_right_wf=.a if calcul_yr_hs_needy_right_wf==0 | calcul_yr_hs_needy_right_wf>5

replace your_estim_yr_hs_needy_hd=.a if your_estim_yr_hs_needy_hd==0 | your_estim_yr_hs_needy_hd>9000
replace your_estim_yr_hs_needy_wf=.a if your_estim_yr_hs_needy_wf==0 | your_estim_yr_hs_needy_wf>9000

replace estim_yr_hs_needy_extra_hd=.a if estim_yr_hs_needy_extra_hd==0 | estim_yr_hs_needy_extra_hd>9000
replace estim_yr_hs_needy_extra_wf=.a if estim_yr_hs_needy_extra_wf==0 | estim_yr_hs_needy_extra_wf >9000

/////

replace calcul_yr_hs_change_hd=.a if calcul_yr_hs_change_hd==0 | calcul_yr_hs_change_hd>6000
replace calcul_yr_hs_change_wf=.a if calcul_yr_hs_change_wf==0  | calcul_yr_hs_change_wf>6000

replace calcul_yr_hs_change_right_hd=.a if calcul_yr_hs_change_right_hd==0 | calcul_yr_hs_change_right_hd>5
replace calcul_yr_hs_change_right_wf=.a if calcul_yr_hs_change_right_wf==0 | calcul_yr_hs_change_right_wf>5

replace your_estim_yr_hs_change_hd=.a if your_estim_yr_hs_change_hd==0 | your_estim_yr_hs_change_hd>9000
replace your_estim_yr_hs_change_wf=.a if your_estim_yr_hs_change_wf==0 | your_estim_yr_hs_change_wf>9000

replace estim_yr_hs_change_extra_hd=.a if estim_yr_hs_change_extra_hd==0 | estim_yr_hs_change_extra_hd>9000
replace estim_yr_hs_change_extra_wf=.a if estim_yr_hs_change_extra_wf==0 | estim_yr_hs_change_extra_wf >9000

/////

replace calcul_yr_hs_other_hd=.a if calcul_yr_hs_other_hd==0 | calcul_yr_hs_other_hd>6000
replace calcul_yr_hs_other_wf=.a if calcul_yr_hs_other_wf==0  | calcul_yr_hs_other_wf>6000

replace calcul_yr_hs_other_right_hd=.a if calcul_yr_hs_other_right_hd==0 | calcul_yr_hs_other_right_hd>5
replace calcul_yr_hs_other_right_wf=.a if calcul_yr_hs_other_right_wf==0 | calcul_yr_hs_other_right_wf>5

replace your_estim_yr_hs_change_hd=.a if your_estim_yr_hs_change_hd==0 | your_estim_yr_hs_change_hd>9000
replace your_estim_yr_hs_change_wf=.a if your_estim_yr_hs_change_wf==0 | your_estim_yr_hs_change_wf>9000

replace estim_yr_hs_other_extra_hd=.a if estim_yr_hs_other_extra_hd==0 | estim_yr_hs_other_extra_hd>9000
replace estim_yr_hs_other_extra_wf=.a if estim_yr_hs_other_extra_wf==0 | estim_yr_hs_other_extra_wf >9000

replace hs_other_religious=.a if hs_other_religious==0 | hs_other_religious>6000
replace hs_other_youth=.a if hs_other_youth==0 | hs_other_youth>6000
replace hs_other_senior=.a if hs_other_senior==0 | hs_other_senior>6000
replace hs_other_health=.a if hs_other_health==0 | hs_other_health>6000
replace hs_other_needy=.a if hs_other_needy==0 | hs_other_needy>6000
replace hs_other_socialchange=.a if hs_other_socialchange==0 | hs_other_socialchange>6000
replace volunteer_type_other_oth=.a if volunteer_type_other_oth==0 
replace hs_type_other=.a if hs_type_other==0 | hs_type_other>6000

generate white=0 if (white_h!=. & sequence_num==1) | (white_w!=. & sequence_num==2)
replace white=1 if white_h==1 & sequence_num==1
replace white=1 if white_w==1 & sequence_num==2

generate black=0 if (black_h!=. & sequence_num==1) | (black_w!=. & sequence_num==2)
replace black=1 if black_h==1 & sequence_num==1
replace black=1 if black_w==1 & sequence_num==2

generate native_amer=0 if (native_amer_h!=. & sequence_num==1) | (native_amer_w!=. & sequence_num==2)
replace native_amer=1 if native_amer_h==1 & sequence_num==1
replace native_amer=1 if native_amer_w==1 & sequence_num==2

generate asian=0 if (asian_h!=. & sequence_num==1) | (asian_w!=. & sequence_num==2)
replace asian=1 if asian_h==1 & sequence_num==1
replace asian=1 if asian_w==1 & sequence_num==2

generate other=0 if (other_h!=. & sequence_num==1) | (other_w!=. & sequence_num==2)
replace other=1 if other_h==1 & sequence_num==1
replace other=1 if other_w==1 & sequence_num==2

generate hispanic=0 if (hispanic_head!=. & sequence_num==1) | (hispanic_wife!=. & sequence_num==2)
replace hispanic=1 if hispanic_head==1 & sequence_num==1
replace hispanic=1 if hispanic_wife==1 & sequence_num==2

gen hrs_worked_hd_group=500 if tot_hs_worked_head>0 & tot_hs_worked_head<750
replace hrs_worked_hd_group=1000 if tot_hs_worked_head>=750 & tot_hs_worked_head<1250
replace hrs_worked_hd_group=1500 if tot_hs_worked_head>=1250 & tot_hs_worked_head<1750
replace hrs_worked_hd_group=2000 if tot_hs_worked_head>=1750

gen full_time_hd=.
replace full_time_hd=0 if hrs_worked_hd_group>0 & hrs_worked_hd_group<2000
replace full_time_hd=1 if hrs_worked_hd_group>=2000

gen hrs_worked_spouse_group=500 if tot_hs_worked_spouse>0 & tot_hs_worked_spouse<750
replace hrs_worked_spouse_group=1000 if tot_hs_worked_spouse>=750 & tot_hs_worked_spouse<1250
replace hrs_worked_spouse_group=1500 if tot_hs_worked_spouse>=1250 & tot_hs_worked_spouse<1750
replace hrs_worked_spouse_group=2000 if tot_hs_worked_spouse>=1750

gen full_time_spouse=.
replace full_time_spouse=0 if hrs_worked_spouse_group>0 & hrs_worked_spouse_group<2000
replace full_time_spouse=1 if hrs_worked_spouse_group>=2000

replace hr_wage_hd=. if  hr_wage_hd==998 | hr_wage_hd==999
replace hr_wage_spouse=. if  hr_wage_spouse==998 | hr_wage_spouse==999

replace tot_wage_hd=. if  tot_wage_hd==9999998 | tot_wage_hd==9999999
replace tot_wage_spouse=. if  tot_wage_spouse==9999998 | tot_wage_spouse==9999999

replace hr_wage_hd=tot_wage_hd/hrs_worked_hd_group if (hr_wage_hd==. | hr_wage_hd==0) & tot_wage_hd!=. & tot_wage_hd!=0 & hrs_worked_hd_group!=. & hrs_worked_hd_group!=0
replace hr_wage_spouse=tot_wage_spouse/hrs_worked_spouse_group if (hr_wage_spouse==. | hr_wage_spouse==0) & tot_wage_spouse!=. & tot_wage_spouse!=0 & hrs_worked_spouse_group!=. & hrs_worked_spouse_group!=0

gen hourly_wage_aftertax_hd=.
replace hourly_wage_aftertax_hd=hr_wage_hd if deductions_allowed==0
replace hourly_wage_aftertax_hd=hr_wage_hd * (1-frate-srate+(frate*srate)) if deductions_allowed>0

gen hourly_wage_aftertax_spouse=.
replace hourly_wage_aftertax_spouse=hr_wage_spouse if deductions_allowed==0
replace hourly_wage_aftertax_spouse=hr_wage_spouse * (1-frate-srate+(frate*srate)) if deductions_allowed>0

gen hourly_wage_aftertax=.
replace hourly_wage_aftertax=hourly_wage_aftertax_hd if sequence_num==1
replace hourly_wage_aftertax=hourly_wage_aftertax_spouse if sequence_num==2

replace hourly_wage_aftertax=hourly_wage_aftertax/10

gen full_time=.
replace full_time=full_time_hd if sequence_num==1
replace full_time=full_time_spouse if sequence_num==2

gen unemployed_head=0 if whether_unemployed_head==5 | whether_unemployed_head==8 | whether_unemployed_head==9
replace unemployed_head=1 if whether_unemployed_head==1

tab unemployed_head

gen unemployed_spouse=0 if whether_unemployed_spouse==5 | whether_unemployed_spouse==8 | whether_unemployed_spouse==9
replace unemployed_spouse=1 if whether_unemployed_spouse==1

tab unemployed_spouse

generate unemployed=0 if (unemployed_head==0 & sequence_num==1) | (unemployed_spouse==0 & sequence_num==2)
replace unemployed=1 if unemployed_head==1 & sequence_num==1
replace unemployed=1 if unemployed_spouse==1 & sequence_num==2


keep family_id sequence_num unique_id_crossys  married sex age sex_head sex_spouse sage page  Hyear Wyear hoursHLB hoursWLB taxprice whether_volunteer_person hours_volunteer_person whether_volunteer_family taxable_inc_hh_gen post_tax_income education_head education_wife state year  children LHS HS some_college college_grad college_plus white black native_amer asian other hispanic whether_itemized whether_donation_religious amount_donation_religious whether_donation_combo amount_donation_combo whether_donation_needy amount_donation_needy whether_donation_health amount_donation_health whether_donation_education amount_donation_education whether_donation_youth amount_donation_youth whether_donation_culture amount_donation_culture whether_donation_community amount_donation_community whether_donation_environment amount_donation_environment whether_donation_peace amount_donation_peace whether_donation_other amount_donation_other donation_type_other_org calcul_yr_hs_reli_hd calcul_yr_hs_reli_wf calcul_yr_hs_reli_right_hd calcul_yr_hs_reli_right_wf your_estim_yr_hs_reli_hd your_estim_yr_hs_reli_wf estim_yr_hs_reli_extra_hd estim_yr_hs_reli_extra_wf calcul_yr_hs_youth_hd calcul_yr_hs_youth_wf calcul_yr_hs_youth_right_hd calcul_yr_hs_youth_right_wf your_estim_yr_hs_youth_hd your_estim_yr_hs_youth_wf estim_yr_hs_youth_extra_hd estim_yr_hs_youth_extra_wf calcul_yr_hs_senior_hd calcul_yr_hs_senior_wf calcul_yr_hs_senior_right_hd calcul_yr_hs_senior_right_wf your_estim_yr_hs_senior_hd your_estim_yr_hs_senior_wf estim_yr_hs_senior_extra_hd estim_yr_hs_senior_extra_wf calcul_yr_hs_health_hd calcul_yr_hs_health_wf calcul_yr_hs_health_right_hd calcul_yr_hs_health_right_wf your_estim_yr_hs_health_hd your_estim_yr_hs_health_wf estim_yr_hs_health_extra_hd estim_yr_hs_health_extra_wf calcul_yr_hs_needy_hd calcul_yr_hs_needy_wf calcul_yr_hs_needy_right_hd calcul_yr_hs_needy_right_wf your_estim_yr_hs_needy_hd your_estim_yr_hs_needy_wf estim_yr_hs_needy_extra_hd estim_yr_hs_needy_extra_wf calcul_yr_hs_change_hd calcul_yr_hs_change_wf calcul_yr_hs_change_right_hd calcul_yr_hs_change_right_wf your_estim_yr_hs_change_hd your_estim_yr_hs_change_wf estim_yr_hs_change_extra_hd estim_yr_hs_change_extra_wf calcul_yr_hs_other_hd calcul_yr_hs_other_wf calcul_yr_hs_other_right_hd calcul_yr_hs_other_right_wf your_estim_yr_hs_change_hd your_estim_yr_hs_change_wf estim_yr_hs_other_extra_hd estim_yr_hs_other_extra_wf hs_other_religious hs_other_youth hs_other_senior hs_other_health hs_other_needy hs_other_socialchange volunteer_type_other_oth hs_type_other total_giving deductions_allowed federalTax_otheritem_plus100 federalTax frate srate stateTax_otheritem_plus100 stateTax hourly_wage_aftertax full_time religion rural unemployed_head unemployed_spouse unemployed

order family_id sequence_num unique_id_crossys   married sex age sex_head sex_spouse sage page  Hyear Wyear hoursHLB hoursWLB taxprice whether_volunteer_person hours_volunteer_person whether_volunteer_family taxable_inc_hh_gen post_tax_income education_head education_wife state year  children LHS HS some_college college_grad college_plus white black native_amer asian other hispanic whether_itemized whether_donation_religious amount_donation_religious whether_donation_combo amount_donation_combo whether_donation_needy amount_donation_needy whether_donation_health amount_donation_health whether_donation_education amount_donation_education whether_donation_youth amount_donation_youth whether_donation_culture amount_donation_culture whether_donation_community amount_donation_community whether_donation_environment amount_donation_environment whether_donation_peace amount_donation_peace whether_donation_other amount_donation_other donation_type_other_org calcul_yr_hs_reli_hd calcul_yr_hs_reli_wf calcul_yr_hs_reli_right_hd calcul_yr_hs_reli_right_wf your_estim_yr_hs_reli_hd your_estim_yr_hs_reli_wf estim_yr_hs_reli_extra_hd estim_yr_hs_reli_extra_wf calcul_yr_hs_youth_hd calcul_yr_hs_youth_wf calcul_yr_hs_youth_right_hd calcul_yr_hs_youth_right_wf your_estim_yr_hs_youth_hd your_estim_yr_hs_youth_wf estim_yr_hs_youth_extra_hd estim_yr_hs_youth_extra_wf calcul_yr_hs_senior_hd calcul_yr_hs_senior_wf calcul_yr_hs_senior_right_hd calcul_yr_hs_senior_right_wf your_estim_yr_hs_senior_hd your_estim_yr_hs_senior_wf estim_yr_hs_senior_extra_hd estim_yr_hs_senior_extra_wf calcul_yr_hs_health_hd calcul_yr_hs_health_wf calcul_yr_hs_health_right_hd calcul_yr_hs_health_right_wf your_estim_yr_hs_health_hd your_estim_yr_hs_health_wf estim_yr_hs_health_extra_hd estim_yr_hs_health_extra_wf calcul_yr_hs_needy_hd calcul_yr_hs_needy_wf calcul_yr_hs_needy_right_hd calcul_yr_hs_needy_right_wf your_estim_yr_hs_needy_hd your_estim_yr_hs_needy_wf estim_yr_hs_needy_extra_hd estim_yr_hs_needy_extra_wf calcul_yr_hs_change_hd calcul_yr_hs_change_wf calcul_yr_hs_change_right_hd calcul_yr_hs_change_right_wf your_estim_yr_hs_change_hd your_estim_yr_hs_change_wf estim_yr_hs_change_extra_hd estim_yr_hs_change_extra_wf calcul_yr_hs_other_hd calcul_yr_hs_other_wf calcul_yr_hs_other_right_hd calcul_yr_hs_other_right_wf your_estim_yr_hs_change_hd your_estim_yr_hs_change_wf estim_yr_hs_other_extra_hd estim_yr_hs_other_extra_wf hs_other_religious hs_other_youth hs_other_senior hs_other_health hs_other_needy hs_other_socialchange volunteer_type_other_oth hs_type_other total_giving deductions_allowed federalTax_otheritem_plus100 federalTax frate srate stateTax_otheritem_plus100 stateTax hourly_wage_aftertax full_time religion rural unemployed_head unemployed_spouse unemployed

cd "$output_data"

save analytic_file_foryear_`i'.dta, replace

}
 
/////////////////////////////////////////////////////////////////////////////


cd "$merge"
use 2011_IUPUI_taxsim_PSID.dta, clear

gen head_married=.
replace head_married=0 if mstat==1
replace head_married=1 if mstat==2

//foreach h of varlist education_wife white_w black_w native_amer_w asian_w other_w hispanic_wife sage          whether_volunteer_religious_wf whether_volunteer_youth_wf whether_volunteer_senior_wf whether_volunteer_health_wf whether_volunteer_needy_wf whether_volunteer_socchange_wf whether_volunteer_other_wf type_volunteer_wf  {
    
//		replace `h'=.a if head_married==0
	
//	}




drop if year==.

//replace frate=frate/100
//replace srate=srate/100 

replace frate=(federalTax_otheritem_plus100-federalTax)/100
replace srate=(stateTax_otheritem_plus100-stateTax)/100

replace frate=0 if frate>0
replace srate=0 if srate>0

sum frate srate

replace frate=abs(frate)
replace srate=abs(srate)


 *generate taxprice 
//gen taxprice=1 if whether_itemized!=1
//replace taxprice=1-frate-srate+(frate*srate) if whether_itemized==1
gen taxprice=1 if deductions_allowed==0
replace taxprice=1-frate-srate+(frate*srate) if deductions_allowed>0



*generate variable for whether anyone in family volunteered
egen whether_volunteer_family=rowmax(Hyear Wyear)
generate whether_volunteer_person=Hyear if sequence_num==1
replace whether_volunteer_person=Wyear if sequence_num==2

*calculate post-tax income
gen post_tax_income_neg=taxable_inc_hh_gen-federalTax-stateTax
gen zero=0
egen post_tax_income=rowmax(post_tax_income_neg zero) if post_tax_income_neg!=.
replace post_tax_income=post_tax_income/100000 

*family income (name of variable for income). put it in thousands
replace taxable_inc_hh_gen=taxable_inc_hh_gen/100000
//format taxable_inc_hh_gen  %9.0g


gen sex_spouse=.
replace sex_spouse=1 if sex_head==2 & head_married==1
replace sex_spouse=2 if sex_head==1 & head_married==1

gen sex=.
replace sex=1 if sex_head==1 & sequence_num==1
replace sex=1 if sex_spouse==1 & sequence_num==2
replace sex=2 if sex_head==2 & sequence_num==1
replace sex=2 if sex_spouse==2 & sequence_num==2


*education variables
generate LHS=0 if (education_head!=. & sequence_num==1) | (education_wife!=. & sequence_num==2)
replace LHS=1 if education_head<=11 & sequence_num==1
replace LHS=1 if education_wife<=11 & sequence_num==2
 
generate HS=0 if (education_head!=. & sequence_num==1) | (education_wife!=. & sequence_num==2)
replace HS=1 if education_head==12 & sequence_num==1
replace HS=1 if education_wife==12 & sequence_num==2

generate some_college=0 if (education_head!=. & sequence_num==1) | (education_wife!=. & sequence_num==2)
replace some_college=1 if (education_head==13 | education_head==14 | education_head==15) & sequence_num==1 & education_head!=.
replace some_college=1 if (education_wife==13 | education_wife==14 | education_wife==15) & sequence_num==2 & education_wife!=.

generate college_grad=0 if (education_head!=. & sequence_num==1) | (education_wife!=. & sequence_num==2)
replace college_grad=1 if education_head==16 & sequence_num==1
replace college_grad=1 if education_wife==16 & sequence_num==2

generate college_plus=0 if (education_head!=. & sequence_num==1) | (education_wife!=. & sequence_num==2)
replace college_plus=1 if education_head==17 & sequence_num==1
replace college_plus=1 if education_wife==17 & sequence_num==2

*egen race_head=rowmax(white_h black_h native_amer_h asian_h  other_h)

/*
replace whether_donation25_religious=.a if whether_donation25_religious==0 | whether_donation25_religious>5
*/
replace whether_donation_religious=.a if whether_donation_religious==0 | whether_donation_religious>5
replace amount_donation_religious=.a if amount_donation_religious==0 | amount_donation_religious>999997
replace whether_donation_combo=.a if whether_donation_combo==0 | whether_donation_combo>5
replace amount_donation_combo=.a if amount_donation_combo==0 | amount_donation_combo>999997
replace whether_donation_needy=.a if whether_donation_needy==0 | whether_donation_needy>5
replace amount_donation_needy=.a if amount_donation_needy==0 | amount_donation_needy>99997
replace whether_donation_health=.a if whether_donation_health==0 | whether_donation_health>5
replace amount_donation_health=.a if amount_donation_health==0 | amount_donation_health>99997
replace whether_donation_education=.a if whether_donation_education==0 | whether_donation_education>5
replace amount_donation_education=.a if amount_donation_education==0 | amount_donation_education>99997
replace whether_donation_youth=.a if whether_donation_youth==0 | whether_donation_youth>5
replace amount_donation_youth=.a if amount_donation_youth==0 | amount_donation_youth>99997
replace whether_donation_culture=.a if whether_donation_culture==0 | whether_donation_culture>5
replace amount_donation_culture=.a if amount_donation_culture==0 | amount_donation_culture>99997
replace whether_donation_community=.a if whether_donation_community==0 | whether_donation_community>5
replace amount_donation_community=.a if amount_donation_community==0 | amount_donation_community>99997
replace whether_donation_environment=.a if whether_donation_environment==0 | whether_donation_environment>5
replace amount_donation_environment=.a if amount_donation_environment==0 | amount_donation_environment>99997
replace whether_donation_peace=.a if whether_donation_peace==0 | amount_donation_peace>5
replace amount_donation_peace=.a if amount_donation_peace==0 | amount_donation_peace>99997
replace whether_donation_other=.a if whether_donation_other==0 | whether_donation_other>5
replace amount_donation_other=.a if amount_donation_other==0 | amount_donation_other>99997
replace whether_volunteer_religious_hd=.a if whether_volunteer_religious_hd==0 | whether_volunteer_religious_hd>5
replace whether_volunteer_youth_hd=.a if whether_volunteer_youth_hd==0 | whether_volunteer_youth_hd>5
replace whether_volunteer_senior_hd=.a if whether_volunteer_senior_hd==0 | whether_volunteer_senior_hd>5
replace whether_volunteer_health_hd=.a if whether_volunteer_health_hd==0 | whether_volunteer_health_hd>5
replace whether_volunteer_needy_hd=.a if whether_volunteer_needy_hd==0 | whether_volunteer_needy_hd>5
replace whether_volunteer_socchange_hd=.a if whether_volunteer_socchange_hd==0 | whether_volunteer_socchange_hd>5
replace whether_volunteer_other_hd=.a if whether_volunteer_other_hd==0 | whether_volunteer_other_hd>5
replace whether_volunteer_religious_wf=.a if whether_volunteer_religious_wf==0 | whether_volunteer_religious_wf>5
replace whether_volunteer_youth_wf=.a if whether_volunteer_youth_wf==0 | whether_volunteer_youth_wf>5
replace whether_volunteer_senior_wf=.a if whether_volunteer_senior_wf==0 | whether_volunteer_senior_wf>5
replace whether_volunteer_health_wf=.a if whether_volunteer_health_wf==0 | whether_volunteer_health_wf>5
replace whether_volunteer_needy_wf=.a if whether_volunteer_needy_wf==0 | whether_volunteer_needy_wf>5
replace whether_volunteer_socchange_wf=.a if whether_volunteer_socchange_wf==0 | whether_volunteer_socchange_wf>5
replace whether_volunteer_other_wf=.a if whether_volunteer_other_wf==0 | whether_volunteer_other_wf>5
      
generate white=0 if (white_h!=. & sequence_num==1) | (white_w!=. & sequence_num==2)
replace white=1 if white_h==1 & sequence_num==1
replace white=1 if white_w==1 & sequence_num==2

generate black=0 if (black_h!=. & sequence_num==1) | (black_w!=. & sequence_num==2)
replace black=1 if black_h==1 & sequence_num==1
replace black=1 if black_w==1 & sequence_num==2

generate native_amer=0 if (native_amer_h!=. & sequence_num==1) | (native_amer_w!=. & sequence_num==2)
replace native_amer=1 if native_amer_h==1 & sequence_num==1
replace native_amer=1 if native_amer_w==1 & sequence_num==2

generate asian=0 if (asian_h!=. & sequence_num==1) | (asian_w!=. & sequence_num==2)
replace asian=1 if asian_h==1 & sequence_num==1
replace asian=1 if asian_w==1 & sequence_num==2

generate other=0 if (other_h!=. & sequence_num==1) | (other_w!=. & sequence_num==2)
replace other=1 if other_h==1 & sequence_num==1
replace other=1 if other_w==1 & sequence_num==2

generate hispanic=0 if (hispanic_head!=. & sequence_num==1) | (hispanic_wife!=. & sequence_num==2)
replace hispanic=1 if hispanic_head==1 & sequence_num==1
replace hispanic=1 if hispanic_wife==1 & sequence_num==2

replace sage=. if sage==0
gen age=.
replace age=page if sequence_num==1
replace age=sage if sequence_num==2

gen religion=religion_hd if sequence_num==1
replace religion=religion_spouse if sequence_num==2
// clean religion 

gen married=0 if  head_married==0
replace married=1 if head_married==1 & sequence_num==1
replace married=1 if sequence_num==2

gen hrs_worked_hd_group=500 if tot_hs_worked_head>0 & tot_hs_worked_head<750
replace hrs_worked_hd_group=1000 if tot_hs_worked_head>=750 & tot_hs_worked_head<1250
replace hrs_worked_hd_group=1500 if tot_hs_worked_head>=1250 & tot_hs_worked_head<1750
replace hrs_worked_hd_group=2000 if tot_hs_worked_head>=1750

gen full_time_hd=.
replace full_time_hd=0 if hrs_worked_hd_group>0 & hrs_worked_hd_group<2000
replace full_time_hd=1 if hrs_worked_hd_group>=2000

gen hrs_worked_spouse_group=500 if tot_hs_worked_spouse>0 & tot_hs_worked_spouse<750
replace hrs_worked_spouse_group=1000 if tot_hs_worked_spouse>=750 & tot_hs_worked_spouse<1250
replace hrs_worked_spouse_group=1500 if tot_hs_worked_spouse>=1250 & tot_hs_worked_spouse<1750
replace hrs_worked_spouse_group=2000 if tot_hs_worked_spouse>=1750

gen full_time_spouse=.
replace full_time_spouse=0 if hrs_worked_spouse_group>0 & hrs_worked_spouse_group<2000
replace full_time_spouse=1 if hrs_worked_spouse_group>=2000

replace hr_wage_hd=. if  hr_wage_hd==998 | hr_wage_hd==999
replace hr_wage_spouse=. if  hr_wage_spouse==998 | hr_wage_spouse==999

replace tot_wage_hd=. if  tot_wage_hd==9999998 | tot_wage_hd==9999999
replace tot_wage_spouse=. if  tot_wage_spouse==9999998 | tot_wage_spouse==9999999

replace hr_wage_hd=tot_wage_hd/hrs_worked_hd_group if (hr_wage_hd==. | hr_wage_hd==0) & tot_wage_hd!=. & tot_wage_hd!=0 & hrs_worked_hd_group!=. & hrs_worked_hd_group!=0
replace hr_wage_spouse=tot_wage_spouse/hrs_worked_spouse_group if (hr_wage_spouse==. | hr_wage_spouse==0) & tot_wage_spouse!=. & tot_wage_spouse!=0 & hrs_worked_spouse_group!=. & hrs_worked_spouse_group!=0

gen hourly_wage_aftertax_hd=.
replace hourly_wage_aftertax_hd=hr_wage_hd if deductions_allowed==0
replace hourly_wage_aftertax_hd=hr_wage_hd * (1-frate-srate+(frate*srate)) if deductions_allowed>0

gen hourly_wage_aftertax_spouse=.
replace hourly_wage_aftertax_spouse=hr_wage_spouse if deductions_allowed==0
replace hourly_wage_aftertax_spouse=hr_wage_spouse * (1-frate-srate+(frate*srate)) if deductions_allowed>0

gen hourly_wage_aftertax=.
replace hourly_wage_aftertax=hourly_wage_aftertax_hd if sequence_num==1
replace hourly_wage_aftertax=hourly_wage_aftertax_spouse if sequence_num==2

replace hourly_wage_aftertax=hourly_wage_aftertax/10

gen full_time=.
replace full_time=full_time_hd if sequence_num==1
replace full_time=full_time_spouse if sequence_num==2

gen unemployed_head=0 if whether_unemployed_head==5 | whether_unemployed_head==8 | whether_unemployed_head==9
replace unemployed_head=1 if whether_unemployed_head==1

tab unemployed_head

gen unemployed_spouse=0 if whether_unemployed_spouse==5 | whether_unemployed_spouse==8 | whether_unemployed_spouse==9
replace unemployed_spouse=1 if whether_unemployed_spouse==1

tab unemployed_spouse

generate unemployed=0 if (unemployed_head==0 & sequence_num==1) | (unemployed_spouse==0 & sequence_num==2)
replace unemployed=1 if unemployed_head==1 & sequence_num==1
replace unemployed=1 if unemployed_spouse==1 & sequence_num==2


											 
keep family_id sequence_num unique_id_crossys  married sex age sex_head sex_spouse sage page Hyear Wyear taxprice whether_volunteer_family whether_volunteer_person taxable_inc_hh_gen post_tax_income education_head education_wife state year  children LHS HS some_college college_grad college_plus white black native_amer asian other hispanic whether_itemized  whether_donation_religious amount_donation_religious whether_donation_combo amount_donation_combo whether_donation_needy amount_donation_needy whether_donation_health amount_donation_health whether_donation_education amount_donation_education whether_donation_youth amount_donation_youth whether_donation_culture amount_donation_culture whether_donation_community amount_donation_community whether_donation_environment amount_donation_environment whether_donation_peace amount_donation_peace whether_donation_other amount_donation_other donation_type_other_org whether_volunteer_religious_hd whether_volunteer_youth_hd whether_volunteer_senior_hd whether_volunteer_health_hd whether_volunteer_needy_hd whether_volunteer_socchange_hd whether_volunteer_other_hd type_volunteer_hd whether_volunteer_religious_wf whether_volunteer_youth_wf whether_volunteer_senior_wf whether_volunteer_health_wf whether_volunteer_needy_wf whether_volunteer_socchange_wf whether_volunteer_other_wf type_volunteer_wf total_giving deductions_allowed federalTax_otheritem_plus100 federalTax frate srate stateTax_otheritem_plus100 stateTax hourly_wage_aftertax full_time religion rural unemployed_head unemployed_spouse unemployed

order family_id sequence_num unique_id_crossys married sex age sex_head sex_spouse sage page Hyear Wyear  taxprice whether_volunteer_family whether_volunteer_person taxable_inc_hh_gen post_tax_income education_head education_wife state year  children LHS HS some_college college_grad college_plus white black native_amer asian other hispanic whether_itemized  whether_donation_religious amount_donation_religious whether_donation_combo amount_donation_combo whether_donation_needy amount_donation_needy whether_donation_health amount_donation_health whether_donation_education amount_donation_education whether_donation_youth amount_donation_youth whether_donation_culture amount_donation_culture whether_donation_community amount_donation_community whether_donation_environment amount_donation_environment whether_donation_peace amount_donation_peace whether_donation_other amount_donation_other donation_type_other_org whether_volunteer_religious_hd whether_volunteer_youth_hd whether_volunteer_senior_hd whether_volunteer_health_hd whether_volunteer_needy_hd whether_volunteer_socchange_hd whether_volunteer_other_hd type_volunteer_hd whether_volunteer_religious_wf whether_volunteer_youth_wf whether_volunteer_senior_wf whether_volunteer_health_wf whether_volunteer_needy_wf whether_volunteer_socchange_wf whether_volunteer_other_wf type_volunteer_wf total_giving deductions_allowed federalTax_otheritem_plus100 federalTax frate srate stateTax_otheritem_plus100 stateTax hourly_wage_aftertax full_time religion rural unemployed_head unemployed_spouse unemployed

cd "$output_data"


save analytic_file_foryear_2011.dta, replace
 
 cd "$dictionaries"
log close
